/*
 * Toedo
 * Copyright (C) 2009-2010 J. Devauchelle and contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package org.toedo.model;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.toedo.model.Task.TaskStatus;

public class Tag extends XStreamSerializableObject {

	private UUID mId;
	private String mName;
	private List<Task> mTasks;
	private List<Note> mNotes;
	
	public Tag(String name) {
		mId = UUID.randomUUID();
		mName = name;
		mTasks = new ArrayList<Task>();
		mNotes = new ArrayList<Note>();
	}
	
	@Override
	protected Object readResolve() {
		if (mTasks == null) {
			mTasks = new ArrayList<Task>();
		}
		
		if (mNotes == null) {
			mNotes = new ArrayList<Note>();
		}
		return this;
	}
	
	public UUID getId() {
		return mId;
	}
	
	public String getName() {
		return mName;
	}
	
	public List<Task> getTasks() {
		return mTasks;
	}
	
	public List<Task> getTasks(boolean includeClosedTaks) {
		if (includeClosedTaks) {
			return mTasks;
		} else {
			List<Task> result = new ArrayList<Task>();
			
			for (Task task : mTasks) {
				if ((task instanceof Task) &&
						(task.getStatus() != TaskStatus.EnClosed)) {
					result.add(task);
				}
			}
			
			return result;
		}
	}
	
	public void addTask(Task task) {
		if (!mTasks.contains(task)) {
			mTasks.add(task);
		}
	}
	
	public void removeTask(Task task) {
		if (mTasks.contains(task)) {
			mTasks.remove(task);
		}
	}
	
	public List<Note> getNotes() {
		return mNotes;
	}
	
	public void addNote(Note note) {
		if (!mNotes.contains(note)) {
			mNotes.add(note);
		}
	}
	
	public void removeNote(Note note) {
		if (mNotes.contains(note)) {
			mNotes.remove(note);
		}
	}
	
}
